package com.sitech.controller;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.sitech.pojo.UserBean;
import com.sitech.service.IUserService;
import com.sitech.util.LoginUtils;

@Controller
@RequestMapping("/login")
public class LoginController{
	@Resource(name="userServiceImpl")
	IUserService iUserService;
	@RequestMapping("/startLogin.html")
	public ModelAndView toLogin(HttpServletRequest request){
		ModelAndView md = new ModelAndView("demo");
		LoginUtils.forwordIndexPage(md,request.getServletPath(),request);
		return md;
	}
	
	@RequestMapping("/login.do")
	public ModelAndView verifyLogin(UserBean user,HttpServletRequest request,HttpServletResponse response){
		System.out.println(user.getUsername()+user.getRemeberMe());
		LoginUtils.remeberMe(user, request, response);
		UserBean bean = iUserService.selectByID(user.getUsername());
		if(bean==null||!bean.equals(user)){
			return  new ModelAndView("error");
		}else{
			request.getSession().setAttribute("username", user.getUsername());
			return new ModelAndView("index");
		}
	}
	@RequestMapping("/register.do")
	public ModelAndView doRegister(UserBean user){
		System.out.println(user.getUsername());
		user.setCreatetime(new Date());
		iUserService.insert(user);	
		return new ModelAndView("index");

	}
}
